# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

# Scripting class with extensions
package dcExplorer::core::Planets;
use strict;
use utf8;
use WeakRef;
use core::Planet;

# Construction methods

sub new 
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = {};
	$self->{Known} = {};
	$self->{Jumpable} = {};
	$self->{My} = {};
	weaken($self->{Parent} = shift);
	bless ($self, $class);
	return $self;
}

sub set
{
	my $self = shift;
	my $planet = shift;
	my $coord = sprintf("%04d:%04d", $planet->getProp('x'), $planet->getProp('y'));
	$self->{Known}->{$coord} = $planet;
	if ($planet->getProp('jumpable')) 
	{ 
		$self->{Jumpable}->{$coord} = $planet; 
	} 
	else 
	{ 
		delete $self->{Jumpable}->{$coord};
	}
	if ($planet->getProp('my'))
	{
		$self->{My}->{$coord} = $planet;
	}
	else
	{
		delete $self->{My}->{$coord};
	}
}

# Internal engine methods

sub getEmpire
{
	my $self = shift;
	return $self->{Parent}->getEmpire();
}

# Scripting interface
	
sub get
{
	my $self = shift;
	my $x = shift;
	my $y = shift;
	my $coord = sprintf("%04d:%04d", $x, $y);
	return $self->{Known}->{$coord} || dcExplorer::core::Planet->new($self, $x, $y);
}

sub getAllMy
{
	my $self = shift;
	my @result = ();
	push @result, $self->{My}->{$_} foreach sort keys %{$self->{My}};
	return @result;
}

sub getAllJumpable
{
	my $self = shift;
	my @result = ();
	push @result, $self->{Jumpable}->{$_} foreach sort keys %{$self->{Jumpable}};
	return @result;
}

sub getAllJumpableQuick
{
	my $self = shift;
	return $self->getAllJumpable();
}

sub getAllKnown
{
	my $self = shift;
	my @result = ();
	push @result, $self->{Known}->{$_} foreach sort keys %{$self->{Known}};
	return @result;
}

sub getAllKnownQuick
{
	my $self = shift;
	return $self->getAllKnown();
}

sub getReacheble
{
	my $self = shift;
	my ($x, $y, $distance) = @_;
	# TODO: Implement.
	die ref($self)."::getReacheble is not implemented yet.";
}

sub getReachebleJumpable
{
	my $self = shift;
	my ($x, $y, $distance) = @_;
	# TODO: Implement.
	die ref($self)."::getReachebleJumpable is not implemented yet.";
}

sub getNearestMy
{
	my $self = shift;
	my ($x, $y) = @_;
	# TODO: Implement.
	die ref($self)."::getNearestMy is not implemented yet.";
}

sub getNearestJumpable
{
	my $self = shift;
	my ($x, $y) = @_;
	# TODO: Implement.
	die ref($self)."::getNearestJumpable is not implemented yet.";
}

sub getRectangle
{
	my $self = shift;
	my ($left, $top, $right, $bottom) = @_;
	my @planets;
	for my $x ($left .. $right)
	{
		for my $y ($top .. $bottom)
		{
			push @planets, $self->get(($x - 1) % 2000 + 1, ($y - 1) % 2000 + 1);
		}
	}
	return @planets;
}

1;
